package com.mtz.controller;

import com.mtz.VO.ResultVO;
import com.mtz.dateobject.KnowledgeType;
import com.mtz.service.KnowledgeTypeService;
import com.mtz.utils.ResultVOUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 煜赜
 * @date 2018/12/17 0017 上午 9:45
 */
@Api(description = "知识类目接口")
@RestController
@RequestMapping("/type")
public class KnowledgeTypeController {

    @Autowired
    private KnowledgeTypeService knowledgeTypeService;

    /**
     * 通过类目id查找类目
     * @param id 类目id
     * @return
     */
    @ApiOperation(value = "通过类目id查找类目", notes = "通过类目id查找类目")
    @GetMapping("/id")
    public ResultVO findById(@ApiParam(value = "类目id",required = true)
                             @RequestParam("id") Integer id) {
        KnowledgeType knowledgeType = knowledgeTypeService.findById(id);
        return ResultVOUtil.success(knowledgeType);
    }

    /**
     * 通过类目名查找类目
     * @param typeName 类目名
     * @return
     */
    @ApiOperation(value = "通过类目名查找类目", notes = "通过类目名查找类目")
    @GetMapping("/typeName")
    public ResultVO findByTypeName(@ApiParam(value = "类目名",required = true)
                             @RequestParam("typeName") String typeName) {
        KnowledgeType knowledgeType = knowledgeTypeService.findByTypeName(typeName);
        return ResultVOUtil.success(knowledgeType);
    }

    /**
     * 查找所有类目
     * @return
     */
    @ApiOperation(value = "查找所有类目", notes = "查找所有类目")
    @GetMapping("/all")
    public ResultVO findAll() {
        List<KnowledgeType> knowledgeTypeList = knowledgeTypeService.findAll();
        return ResultVOUtil.success(knowledgeTypeList);
    }

    /**
     * 增加类目
     * @param typeName 类目名
     * @return
     */
    @ApiOperation(value = "增加类目", notes = "增加类目")
    @PostMapping("/add")
    public ResultVO addType(@ApiParam(value = "类目名",required = true)
                                @RequestParam("typeName") String typeName) {
        knowledgeTypeService.addKnowledgeType(typeName);
        return ResultVOUtil.success();
    }

    /**
     * 修改类目
     * @param id 类目id
     * @param typeName typeName
     * @return
     */
    @ApiOperation(value = "修改类目", notes = "修改类目")
    @PostMapping("/edit")
    public ResultVO editType(@ApiParam(value = "类目id",required = true)
                                 @RequestParam("id") Integer id,
                             @ApiParam(value = "类目名",required = true)
                                 @RequestParam("typeName") String typeName) {
        knowledgeTypeService.editKnowledgeType(id, typeName);
        return ResultVOUtil.success();
    }

    /**
     * 删除类目
     * @param id 类目id
     * @return
     */
    @ApiOperation(value = "删除类目", notes = "删除类目")
    @PostMapping("/deleteTrue")
    public ResultVO deleteType(@ApiParam(value = "类目id",required = true)
                             @RequestParam("id") Integer id) {
        knowledgeTypeService.deleteKnowledgeType(id);
        return ResultVOUtil.success();
    }
}
